package com.mrx.java8.optional.exercise;

import org.junit.jupiter.api.Test;

import java.util.Optional;

public class TestOptional {

    @Test
    public void test1(){
        Man man = new Man();
        String name = getGodnessName(man);
        System.out.println(name);
    }

    // 需求: 获取一个男人心中女神的名字
    public String getGodnessName(Man man){
        //return man.getGodness().getName();
        if(man != null){
            Godness godness = man.getGodness();
            if(godness != null){
                return godness.getName();
            }
        }
        return "Hello World";
    }



    @Test
    public void test2(){
        Optional<Godness> gn = Optional.ofNullable(new Godness("你好"));
        Optional<NewMan> man = Optional.ofNullable(new NewMan(gn));
        String name = getGodnessName2(man);
        System.out.println(name);
    }
    public String getGodnessName2(Optional<NewMan> man){
        return man.orElse(new NewMan())
                .getGodness()
                .orElse(new Godness("Hello Optional"))
                .getName();
    }
}
